import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
from matplotlib.ticker import MaxNLocator#设置坐标为整型
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题

#导入数据
data_Wuhan = np.array(pd.read_excel('environment.xlsx','Wuhan')).tolist()
data_Nanjing = np.array(pd.read_excel('environment.xlsx','Nanjing')).tolist()
data_Guangzhou = np.array(pd.read_excel('environment.xlsx','Guangzhou')).tolist()
data_Shenzhen = np.array(pd.read_excel('environment.xlsx','Shenzhen')).tolist()
data_Beijing = np.array(pd.read_excel('environment.xlsx','Beijing')).tolist()
data_Shanghai = np.array(pd.read_excel('environment.xlsx','Shanghai')).tolist()
data_all = np.array(pd.read_excel('environment.xlsx','ALL')).tolist()

#绘制改进的堆积条形图描述PM2.5
x_lable = ['武汉','南京','广州','深圳','北京','上海']
y_2018 = [data_Wuhan[0][1],data_Nanjing[0][1],data_Guangzhou[0][1],data_Shenzhen[0][1],data_Beijing[0][1],data_Shanghai[0][1]]
y_2019 = [data_Wuhan[1][1],data_Nanjing[1][1],data_Guangzhou[1][1],data_Shenzhen[1][1],data_Beijing[1][1],data_Shanghai[1][1]]
y_2020 = [data_Wuhan[2][1],data_Nanjing[2][1],data_Guangzhou[2][1],data_Shenzhen[2][1],data_Beijing[2][1],data_Shanghai[2][1]]
print(y_2020)
print(data_Shanghai[2][1])
y_2021 = [data_Wuhan[3][1],data_Nanjing[3][1],data_Guangzhou[3][1],data_Shenzhen[3][1],data_Beijing[3][1],data_Shanghai[3][1]]
plt.bar(x_lable, y_2018, align='center', color='#66c2a5', tick_label=x_lable, label='2018年')
for a, b in zip(x_lable,y_2018):
    plt.text(a, b + 0.1, b, ha='center', va='bottom')
plt.bar(x_lable, y_2019, align='center', color='#8da0cb', label='2019年')
for a, b in zip(x_lable,y_2019):
    plt.text(a, b + 0.1, b, ha='center', va='bottom')
plt.bar(x_lable, y_2020, align='center',  color='#8696a7', label='2020年')
for a, b in zip(x_lable,y_2020):
    plt.text(a, b + 0.1, b, ha='center', va='bottom')
plt.bar(x_lable, y_2021, align='center',  color='#c1cbd7', label='2021年')
for a, b in zip(x_lable,y_2021):
    plt.text(a, b + 0.1, b, ha='center', va='bottom')
plt.xlabel('城市')
plt.ylabel('PM2.5浓度（%）')
plt.title("六大城市2018-2021年PM2.5浓度变化")
plt.legend()
plt.show()

#绘制折线图描述空气达标率
x = range(2018,2022)
y_Guangzhou = [l[2] for l in data_Guangzhou]
y_Shenzhen = [l[2] for l in data_Shenzhen]
y_Nanjing = [l[2] for l in data_Nanjing]
y_Wuhan = [l[2] for l in data_Wuhan]
y_Beijing = [l[2] for l in data_Beijing]
y_Shanghai = [l[2] for l in data_Shanghai]
plt.xlabel('年份')
plt.ylabel('空气质量达标率（%）')
plt.plot(x, y_Wuhan, lw=2, c='indianred', marker='s', ms=4, label='武汉')
plt.plot(x, y_Nanjing, lw=2, c='sandybrown', marker='o', ms=4, label='南京')
plt.plot(x, y_Guangzhou, lw=2, c='darkseagreen', marker='v', ms=4, label='广州')
plt.plot(x, y_Shenzhen, lw=2, c='cadetblue', marker='x', ms=4, label='深圳')
plt.plot(x, y_Beijing, lw=2, c='slategray', marker='*', ms=4, label='北京')
plt.plot(x, y_Shanghai, lw=2, c='mediumaquamarine', marker='1', ms=4, label='上海')
plt.legend(['武汉','南京','广州','深圳','北京','上海'])
plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True))
plt.title("六大城市2018-2021年空气质量达标率对比")
plt.show()

#绘制堆叠面积图描述人均绿地面积
x = range(2018,2022)
y_greenArea = {
    '广州':[l[3] for l in data_Guangzhou],
    '南京':[l[3] for l in data_Nanjing],
    '北京':[l[3] for l in data_Beijing],
    '上海':[l[3] for l in data_Shanghai],
    '深圳':[l[3] for l in data_Shenzhen],
    '武汉':[l[3] for l in data_Wuhan],
}

fig,ax = plt.subplots()
df = pd.DataFrame(y_greenArea,index = range(2018,2022))
df.plot.area(colormap = 'Greens_r',alpha=1,ax=ax)
plt.gca().xaxis.set_major_locator(MaxNLocator(integer=True))
ax.legend(loc='upper right')
ax.set_xlabel('年份')
ax.set_ylabel('人均绿地面积（人/平方米）')
plt.title("六大城市2018-2021年人均绿地面积对比")
plt.show()

#绘制雷达图对城市综合评估:gdp 房价 收入 环境 城市资源（2021年对比）
#风格美化
plt.style.use('ggplot')

#构造数据（共六组）
value = [[],[],[],[],[],[]]
for j in range(0,6):
    i = j+1
    value[j] = [data_all[0][1]/data_all[i][1],data_all[i][2]/data_all[0][2],data_all[i][3]/data_all[0][3],data_all[i][4]*6.5+data_all[i][5]*1.5+data_all[i][6]*2,data_all[i][7]/data_all[0][7]*5+data_all[i][8]/data_all[0][8]*5]
# print(value[0])

feature = ['GDP','房价','收入','城市环境','城市资源']
labels = np.array(feature)
# 设置每个数据点的显示位置，在雷达图上用角度表示
angles = np.linspace(0, 2 * np.pi, 5, endpoint=False)
angles = np.concatenate((angles, [angles[0]]))
labels=np.concatenate((labels,[labels[0]])) #封闭

data = np.concatenate((np.array(value[0]),[np.array(value[0])[0]]))
data_1 = np.concatenate((np.array(value[1]),[np.array(value[1])[0]]))
fig = plt.figure(facecolor="white")
plt.subplot(111,polar=True)
color=['sandybrown','darkseagreen','indianred','cadetblue','slategray','mediumaquamarine']
label = ['武汉','南京','广州','深圳','上海','北京']
for i in range(0,6):
    data = np.concatenate((np.array(value[i]), [np.array(value[i])[0]]))
    plt.plot(angles, data, 'bo-', color=color[i], linewidth=2, label=label[i])
    plt.fill(angles, data, facecolor=color[i], alpha=0.25)

plt.thetagrids(angles*180/np.pi,labels)
plt.figtext(0.52,0.95,'2021城市各项指标雷达图',ha='center')
plt.legend(loc='upper right')
plt.grid(True)
plt.show()